/**
 * Cell
 * Define Cell objects
 **/
var Cell = function() {
	// Game objects
	this.walkable = true;
	this.bounds = {
		w: 0,
		h: 0
	};
	this.position = {
		x: 0,
		y: 0
	}
	// Pathfinding
	this.path = {
		G:0,
		H:0,
		P:null
	}
	this.highlight = false;
	this.color = 'white';
	this.object = null;

	this.init = function(b, p) {
		log('*** INIT CELL ***');
		this.bounds.w = b.w;
		this.bounds.h = b.h;
		this.position.x = p.x;
		this.position.y = p.y;
		// log(this.toString());
	}

	this.addObject = function(o) {
		this.object = o;
		this.object.init(this.position);
		this.color = this.object.getColor();
		this.walkable = this.object.isWalkable();
	}

	this.toString = function() {
		var str = 'Position : ['+this.getX()+', '+this.getY()+'] - Bounds : ['+this.getW()+'x'+this.getH()+'] - ';
		str += this.isWalkable() ? 'Walkable' : 'Not Walkable';
		str += '[G:'+this.path.G+' - H:'+this.path.H+' - F:'+this.getF()+' - Parent:('+this.path.P.getX()+','+this.path.P.getY()+')]';
		return str;
	}

	this.clear = function() {
	}

	this.update = function() {
		try {
			this.object.update();
		}
		catch(e) {}
	}

	this.render = function() {
		var cW = this.getW();
		var cH = this.getH();
		var x = this.getX() * this.getW();
		var y = this.getY() * this.getH();

		CTX.beginPath();
		CTX.rect(x,y,cW,cH);
		CTX.fillStyle = this.color;
		CTX.fill();
		CTX.closePath();

		// this.drawCoef();
	}

	this.getPosition = function(p) {
		return this.map.getPosition(p);
	}

	this.isWalkable = function() {
		return this.walkable;
	}

	this.getW = function() { return this.bounds.w; }
	this.getH = function() { return this.bounds.h; }
	this.getX = function() { return this.position.x; }
	this.getY = function() { return this.position.y; }

	this.onClick = function() {
		// Si la case comporte un objet, on déclenche son action
		if (this.object != null) {
			try {
				this.object.onClick();
			}
			catch(e) { warn(e); }
		}
		else {
			var unit = game.mUnit.getUnitWithPosition(this.position);
			// Sinon, si la case comporte une unité, on déclenche son action
			if (unit != null){
				unit.onClick();
			}
			// Sinon, si on peut se déplacer sur la cellule, on change la cible des unités sélectionnées
			else if (this.isWalkable()) {
				var selectedUnits = game.mUnit.getSelected();
				for (var i = 0; i < selectedUnits.length; i++) {
					selectedUnits[i].setTarget(this.position);
				};
			}
		}
	}

	// Check Cell
	this.isWalkable = function() {
		return this.walkable;
	}

	// Pathfinding
	this.getF = function() {
		return (this.path.G + this.path.H);
	}

	this.drawCoef = function() {
		CTX.fillStyle = 'black';
		// F in top left corner
		var offsetX = 5;
		var offsetY = 10;
		var x = (this.getX() * this.getW()) + offsetX;
		var y = (this.getY() * this.getH()) + offsetY;
		CTX.fillText(this.getF(), x, y);

		// G in bottom left corner
		offsetX = 5;
		offsetY = 5;
		x = (this.getX() * this.getW()) + offsetX;
		y = (this.getY() * this.getH()) + (this.getH() - offsetY);
		CTX.fillText(this.path.G, x, y);

		// H in bottom right corner
		offsetX = 15;
		offsetY = 5;
		x = (this.getX() * this.getW()) + (this.getW() - offsetX);
		y = (this.getY() * this.getH()) + (this.getH() - offsetY);
		CTX.fillText(this.path.H, x, y);
	}

	this.setParent = function(cell) {
		this.path.P = cell;
	}
};
